package com.chat.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chat.domain.pojo.FriendRequest;
import com.chat.domain.pojo.User;
import com.chat.domain.pojo.UserFriend;
import com.chat.domain.vo.FriendRequestVO;
import com.chat.excpetion.CustomException;
import com.chat.mapper.FriendRequestMapper;
import com.chat.mapper.UserFriendMapper;
import com.chat.service.FriendService;
import com.chat.service.UserService;
import com.chat.util.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wb
 * @since 2025-06-07
 */

@Service
public class FriendServiceImpl implements FriendService {

    @Autowired
    private FriendRequestMapper friendRequestMapper;
    @Autowired
    private UserFriendMapper userFriendMapper;
    @Autowired
    private UserService userService;

    @Override
    public Boolean addFriend(Integer userId, String friendInfo, String verifyMsg) {
        boolean b = friendInfo.matches("-?\\d+?");
        Integer receiverId;

        if (b) {
            receiverId = Integer.parseInt(friendInfo);
        } else {
            User user = userService.getUserByUsername(friendInfo);
            if (user == null) {
                throw new CustomException(ResultCodeEnum.FRIEND_REQUEST_ERROR);
            }
            receiverId = user.getId();
        }

        FriendRequest friendRequest = new FriendRequest();
        friendRequest.setSenderId(userId);
        friendRequest.setReceiverId(receiverId);
        friendRequest.setVerifyMsg(verifyMsg);

        return friendRequestMapper.insert(friendRequest) > 0;
    }

    @CacheEvict(value = "friends", allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean respondFriend(Integer id, Integer status) {
        FriendRequest friendRequest = new FriendRequest();
        friendRequest.setId(id);
        friendRequest.setStatus(status);
        FriendRequest friend = friendRequestMapper.selectById(id);

        if (status.equals(1)) {
            UserFriend userFriend1 = new UserFriend();
            userFriend1.setUserId(friend.getSenderId());
            userFriend1.setFriendId(friend.getReceiverId());
            userFriendMapper.insert(userFriend1);

            UserFriend userFriend2 = new UserFriend();
            userFriend2.setUserId(friend.getReceiverId());
            userFriend2.setFriendId(friend.getSenderId());
            userFriendMapper.insert(userFriend2);
        }

        return friendRequestMapper.updateById(friendRequest) > 0;
    }

    @Override
    public List<FriendRequestVO> getFriendRequests(Integer userId) {
        return friendRequestMapper.selectFriendRequests(userId);
    }

    @CacheEvict(value = "friends", allEntries = true)
    @Override
    public Boolean deleteFriend(Integer userId, Integer friendId) {
        LambdaQueryWrapper<UserFriend> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserFriend::getUserId, userId);
        lqw.eq(UserFriend::getFriendId, friendId);

        return userFriendMapper.delete(lqw) > 0;
    }
}
